#include "head.h"

int net_cd(request_t *request, int net_fd) {
    char full_path[1024] = {0};
    getcwd(full_path, sizeof(full_path));
    strcat(full_path, "/");
    strcat(full_path, request->user_name);
    strcat(full_path, "/");
    strcat(full_path, request->path);
    // strcat(full_path, "/");
    strcat(full_path, request->args);
    printf("%s\n", full_path);

    response_t response;
    memset(&response, 0, sizeof(response));

    // 判断传入的路径是否存在
    if (access(full_path, F_OK) == -1) {
        // 路径不存在，返回错误信息
        response.status = 1;
        strcpy(response.args, "No such file or directory");
        send(net_fd, &response, sizeof(response), 0);
        return 0;
    }

    response.status = 0;
    strcpy(response.args, request->args);

    send(net_fd, &response, sizeof(response), 0);
    printf("响应了cd请求\n");
    printf("发送args: %s\n", response.args);
    return 0;
}

// 测试
// int main(void) {
//     request_t request;
//     strcpy(request.user_name, "moxuan");
//     strcpy(request.path, "/home/moxuan");
//     strcpy(request.args, "/test");

//     net_cd(&request, 0);

//     return 0;
// }